package com.chengwang.bookstory.controller;

import com.chengwang.bookstory.pojo.AllOrder;
import com.chengwang.bookstory.pojo.Manager;
import com.chengwang.bookstory.pojo.User;
import com.chengwang.bookstory.service.OrderService;
import com.chengwang.bookstory.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    //注册用户
    @RequestMapping("/registUser")
    public ModelAndView registUser(User user){
        System.out.println(user);
        ModelAndView mv = new ModelAndView();
        Boolean b=userService.uRegist(user);
        if(b){
            mv.addObject("mes","请去qq邮箱激活");
            mv.setViewName("redirect:/regist.html");
            return mv;
        }else {
            mv.addObject("mes","该账号已存在，请重新注册");
            mv.setViewName("redirect:/regist.html");
            return mv;
        }
    }
    //激活用户
    @RequestMapping("/active")
    public ModelAndView active(String userAccount){
        ModelAndView mv = new ModelAndView();
        userService.active(userAccount);
        mv.addObject("mes","您已激活，可以登录");
        mv.setViewName("redirect:/login.html");
        return mv;
    }
    //用户登录
    @RequestMapping("/uLogin")
    public ModelAndView uLogin(User user , Model model ,HttpSession session){
        ModelAndView mv = new ModelAndView();
        System.out.println(user);
        User user1=userService.uLogin(user);
        System.out.println(user1);
        if(user1==null){
            model.addAttribute("mes","用户名或密码错误");
            mv.setViewName("redirect:/login.html");
            return mv;
        }else {
            session.setAttribute("user",user);
            mv.setViewName("index.html");
            return mv;
        }
    }
    //查询用户
    @RequestMapping("/queryUser")
    @ResponseBody
    public List<User> queryUser(Model model , HttpSession session){
        List<User> userList=userService.queryUser();
//        model.addAttribute("userList",userList);
//        Manager manager = (Manager) session.getAttribute("manager");
//        model.addAttribute("manager",manager);
//        model.addAttribute("mes","成功传值");
        return userList;
    }

    //查询用户
    @RequestMapping("/queryUser1")
    public ModelAndView queryUser1(ModelAndView mv , HttpSession session){
        List<User> userList=userService.queryUser();
        mv.addObject("userList",userList);

        Manager manager = (Manager) session.getAttribute("manager");
        mv.addObject("manager",manager);
        mv.setViewName("userList.html");
        return mv;
    }



    //返回首页
    @RequestMapping("/toIndex")
    public String toIndex(){
        return "index";
    }

    //修改个人信息
    @RequestMapping("/updateUser")
    public ModelAndView updateUser(User user){
        ModelAndView mv=new ModelAndView();
        userService.updateUser(user);
        User user1=userService.queryUserByUserAccount(user.getUserAccount());
        mv.addObject("userUp",user1);
        List<AllOrder> orders=orderService.queryAllOrder(user.getUserAccount());
        mv.addObject("orders",orders);
        mv.setViewName("person.html");
        return mv;
    }
    //个人中心
    @RequestMapping("/own")
    public ModelAndView own(HttpSession session){
        ModelAndView mv=new ModelAndView();
        User user = (User) session.getAttribute("user");
        if (user==null){
            mv.setViewName("redirect:/login.html");
            return mv;
        }
        User user1=userService.queryUserByUserAccount(user.getUserAccount());
        mv.addObject("userUp",user1);
        List<AllOrder> orders=orderService.queryAllOrder(user.getUserAccount());
        mv.addObject("orders",orders);
        mv.setViewName("person.html");
        return mv;
    }

    //拉黑用户
    @RequestMapping("/toOff")
    @ResponseBody
    public void toOff(HttpServletRequest request){
        String userAccount = request.getParameter("userAccount");
        userService.updateStatus(userAccount);
    }
    //解除拉黑
    @RequestMapping("/toOpen")
    @ResponseBody
    public void toOpen(String userAccount){
        userService.toOpen(userAccount);

    }
}
